package com.sinodoggo.shitcloud.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinodoggo.shitcloud.commons.Result;
import com.sinodoggo.shitcloud.entity.Role;
import com.sinodoggo.shitcloud.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sinodoggo
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private IRoleService roleService;

    @DeleteMapping("/{id}")
    public Result deleteRoleById(@PathVariable Integer id) {
        roleService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatchByIds(@RequestBody List<Integer> ids) {
        roleService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result getAllRoles() {
        final List<Role> roleList = roleService.list();
        return Result.success(roleList);
    }

    @GetMapping("/info/{id}")
    public Result findSpecificRoleById(@PathVariable("id") Integer id) {
        final Role role = roleService.getById(id);
        return Result.success(role);
    }

    @PostMapping
    public Result saveOrUpdateRole(@RequestBody Role role) {
        roleService.saveOrUpdate(role);
        return Result.success();
    }

    @GetMapping("/page")
    public Result pageDivision(@RequestParam Integer pageNumber,
                               @RequestParam Integer pageSize,
                               @RequestParam(value = "name", defaultValue = "") String name) {
        Page<Role> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (!StrUtil.isBlank(name)) {
            wrapper.like("name", name);
        }
        wrapper.orderByDesc("id");
        return Result.success(roleService.page(page, wrapper));
    }
}

